Convert Sorted Array to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.
题目大意:给定排好序的数组,构造二叉搜索树
题目难度:Medium
import java.util.*;
/**
* Created by gzdaijie on 16/6/8
* 递归地将数组划分为左右2个部分,分别作为左子树和右子树
*/
public class Solution {
public TreeNode sortedArrayToBST(int[] nums) {
return helper(nums, 0, nums.length - 1);
}
public TreeNode helper(int[] nums, int start, int end) {
if (start > end) return null;
int mid = (start + end) / 2;
TreeNode node = new TreeNode(nums[mid]);
node.left = helper(nums, start, mid - 1);
node.right = helper(nums, mid + 1, end);
return node;
}
}